package com.peanutplan.blog.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.peanutplan.blog.constant.WebConst;

/**
 * 添加文章默认封面
 * @author motisy
 *
 */
@Component
public class DefaultCover implements CommandLineRunner {
	public static Map<Integer, String> DEFAULT_ARTICLE_COVER_MAP = new HashMap<>();
    
	@Override
	public void run(String... args) throws Exception {
		int size = 0;
    	try {
			String profile = WebConst.defaultArticleCover;
			File default_article_cover_dir = FileUtils.getFile(profile);
			if(default_article_cover_dir.exists()) {
				File[] images = default_article_cover_dir.listFiles();
				if(images.length>0) {
					for(File img:images) {
						size++;
						DEFAULT_ARTICLE_COVER_MAP.put(size, "/cover/"+img.getName());
					}
				}
			}else {
				default_article_cover_dir.mkdirs();
			}
		} catch (Exception e) {
    		for(int i=1; i<=5; i++) {
    			size++;
				DEFAULT_ARTICLE_COVER_MAP.put(size, "/yqq/images/a0"+i+".jpg");
			}
		}
    	
    	if(DEFAULT_ARTICLE_COVER_MAP.size()==0) {
    		for(int i=1; i<=5; i++) {
    			size++;
				DEFAULT_ARTICLE_COVER_MAP.put(size, "/yqq/images/a0"+i+".jpg");
			}
    	}
	}
}
